<template>
	<view class="content">
		<view class="tips">
			<view>玩家:</view>
			<view>HP:{{player.hp}}</view>
			<view>ACK:{{player.ack}}</view>
			<view>DEF:{{player.def}}</view>
			<view>LEVEL:{{player.level}}</view>
			<view>EXP:{{player.exp}}</view>
			<view>敌人:</view>
			<view>HP:{{fightData.enemy.hp}}</view>
		</view>
		<view class="btn-list">
			<button type="primary" @click="start">开始战斗</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				levelData: [{
						id: 2,
						exp: [10, 50],
						hp: 5,
						ack: 2,
						def: 2,
					},
					{
						id: 3,
						exp: [50, 100],
						hp: 55,
						ack: 12,
						def: 12,
					},
				],
				player: {
					name: "句心",
					hp: 100,
					ack: 5,
					def: 3,
					level: 1,
					exp: 0,
				},
				enemy: {
					name: "哥布林",
					hp: 7,
					ack: 3,
					def: 2,
					level: 1,
					exp: 9,
				},
				fightData: {
					player: {
						hp: 0,
						dmg: 0,
						def: 0
					},
					enemy: {
						hp: 0,
						dmg: 0,
						def: 0
					}
				}
			}
		},
		onLoad: () => {},
		methods: {
			start() {
				this.fight()
			},
			fight() {
				const indexInterval = setInterval(() => {
					console.log('遇到敌人');
					this.enemy.hp = this.getRandom(1,20)
					this.enemy.ack = this.getRandom(1,20)
					this.enemy.def = this.getRandom(1,20)
					this.enemy.level = this.getRandom(1,5)
					this.enemy.exp = this.getRandom(1,20)
					
					//初始化
					this.fightData.player.hp = this.player.hp
					this.fightData.player.dmg = this.player.ack
					this.fightData.player.def = this.player.def
					let player = this.fightData.player
					
					this.fightData.enemy.hp = this.enemy.hp
					this.fightData.enemy.dmg = this.enemy.ack
					this.fightData.enemy.def = this.enemy.def
					let enemy = this.fightData.enemy
					
					console.log('战斗开始');
					
					//玩家先攻击敌人
					player.dmg = this.getPositiveNumber(this.player.ack - this.enemy.def)
					enemy.hp = this.getPositiveNumber(enemy.hp - player.dmg)
					console.log(`${this.player.name}攻击了${this.enemy.name},造成了${player.dmg}点伤害`);
					if (enemy.hp < 1) {
						console.log(`${this.enemy.name}被击败`)
						//计算获取经验值
						this.player.exp += this.enemy.exp
						console.log(`${this.player.name}获取经验值[${this.enemy.exp}]`)
						
						for(let levelItem of this.levelData){
							if(levelItem.exp[0] > this.player.exp){
								//当前经验值不够升级
								break
							}
							if(levelItem.exp[0] <= this.player.exp && this.player.exp < levelItem.exp[1]){
								if(levelItem.id <= this.player.level){
									break
								}
								this.player.level = levelItem.id
								console.log(`${this.player.name}已升级到等级[${this.player.level}]`)
								this.player.hp += levelItem.hp
								this.player.ack += levelItem.ack
								this.player.def += levelItem.def
								console.log(`并获得属性加成[HP+${levelItem.hp}],[ACK+${levelItem.ack}],[DEF+${levelItem.def}]`)
								break
							}
						}
						
						clearInterval(indexInterval)
						return false
					}
					console.log(`${this.enemy.name}还剩HP[${enemy.hp}]`)

					//敌人攻击玩家
					enemy.dmg = this.getPositiveNumber(this.enemy.ack - this.player.def)
					player.hp = this.getPositiveNumber(player.hp - enemy.dmg)
					console.log(`${this.enemy.name}攻击了${this.player.name},造成了${enemy.dmg}点伤害\n`);
					if (player.hp < 1) {
						console.log(`${this.player.name}被击败,游戏结束`)
						clearInterval(indexInterval)
						return false
					}
					console.log(`${this.player.name}还剩HP[${player.hp}]`)
				}, 1000)
			},
			levelUp() {

			},
			//是否存活
			isAlive() {
				return this.fightData.player.hp > 0
			},
			//获取正数
			getPositiveNumber(number) {
				return number < 0 ? 0 : number
			},
			/**
			 * 产生随机整数，包含下限值，但不包括上限值
			 * @param {Number} lower 下限
			 * @param {Number} upper 上限
			 * @return {Number} 返回在下限到上限之间的一个随机整数
			 */
			getRandom(lower, upper) {
				return Math.floor(Math.random() * (upper - lower)) + lower
			},
		}
	}
</script>

<style>

</style>
